using AnimCollections;
using System.Runtime.CompilerServices;
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Playables;

namespace DA.AnimGraph {

    [BurstCompile]
    public static class NodeUtility {
        [BurstCompile]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public unsafe static void ClearWeights(ref NativeSimpleArray<float> weights, int length) {
            var addr = weights.GetUnsafePtr();
            UnsafeUtility.MemClear(addr, length * sizeof(float));
        }

        [BurstCompile]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void SetWeight(in Playable playable, int inputIndex, float weight, ref NodeLink child) {
            // 先断开再 SetInputWeight 会崩溃

            playable.SetInputWeight(inputIndex, weight);

            //bool isSwitch = false;
            //if (weight == 0f) {
            //    if (child.isConnected == ByteBool.True) {
            //        playable.DisconnectInput(inputIndex);
            //        child.isConnected = ByteBool.False;
            //        isSwitch = true;
            //    }
            //} else {
            //    if (child.isConnected == ByteBool.False) {
            //        playable.ConnectInput(inputIndex, child.playable, 0);
            //        child.isConnected = ByteBool.True;
            //        isSwitch = true;
            //    }
            //}
        }
    }
}
